<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
          <el-form-item label="用户名" prop="username">
          <el-input v-model="dataForm.username" placeholder="用户名"></el-input>
      </el-form-item>
          <el-form-item label="昵称" prop="avatar">
          <el-input v-model="dataForm.avatar" placeholder="昵称"></el-input>
      </el-form-item>
          <el-form-item label="0未知，1男，2女" prop="gender">
          <el-input v-model="dataForm.gender" placeholder="0未知，1男，2女"></el-input>
      </el-form-item>
          <el-form-item label="省份" prop="province">
          <el-input v-model="dataForm.province" placeholder="省份"></el-input>
      </el-form-item>
          <el-form-item label="市区" prop="city">
          <el-input v-model="dataForm.city" placeholder="市区"></el-input>
      </el-form-item>
          <el-form-item label="小程序openid" prop="openid">
          <el-input v-model="dataForm.openid" placeholder="小程序openid"></el-input>
      </el-form-item>
          <el-form-item label="个人介绍" prop="intro">
          <el-input v-model="dataForm.intro" placeholder="个人介绍"></el-input>
      </el-form-item>
          <el-form-item label="积分" prop="integral">
          <el-input v-model="dataForm.integral" placeholder="积分"></el-input>
      </el-form-item>
          <el-form-item label="经验，跟用户等级挂钩" prop="exp">
          <el-input v-model="dataForm.exp" placeholder="经验，跟用户等级挂钩"></el-input>
      </el-form-item>
          <el-form-item label="最后登录IP" prop="lastLoginIp">
          <el-input v-model="dataForm.lastLoginIp" placeholder="最后登录IP"></el-input>
      </el-form-item>
          <el-form-item label="用户标签" prop="tagStr">
          <el-input v-model="dataForm.tagStr" placeholder="用户标签"></el-input>
      </el-form-item>
          <el-form-item label="生日" prop="birthday">
          <el-input v-model="dataForm.birthday" placeholder="生日"></el-input>
      </el-form-item>
          <el-form-item label="帖子数量" prop="postNum">
          <el-input v-model="dataForm.postNum" placeholder="帖子数量"></el-input>
      </el-form-item>
          <el-form-item label="粉丝数量" prop="fansNum">
          <el-input v-model="dataForm.fansNum" placeholder="粉丝数量"></el-input>
      </el-form-item>
          <el-form-item label="关注数量" prop="followNum">
          <el-input v-model="dataForm.followNum" placeholder="关注数量"></el-input>
      </el-form-item>
          <el-form-item label="0为普通用户，1官方账号  2马甲虚拟用户" prop="type">
          <el-input v-model="dataForm.type" placeholder="0为普通用户，1官方账号  2马甲虚拟用户"></el-input>
      </el-form-item>
          <el-form-item label="状态" prop="status">
          <el-input v-model="dataForm.status" placeholder="状态"></el-input>
      </el-form-item>
          <el-form-item label="vip过期时间" prop="vipExpireTime">
          <el-input v-model="dataForm.vipExpireTime" placeholder="vip过期时间"></el-input>
      </el-form-item>
          <el-form-item label="" prop="updateTime">
          <el-input v-model="dataForm.updateTime" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="" prop="createTime">
          <el-input v-model="dataForm.createTime" placeholder=""></el-input>
      </el-form-item>
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: '',
        username: '',
        avatar: '',
        gender: '',
        province: '',
        city: '',
        openid: '',
        intro: '',
        integral: '',
        exp: '',
        lastLoginIp: '',
        tagStr: '',
        birthday: '',
        postNum: '',
        fansNum: '',
        followNum: '',
        type: '',
        status: '',
        vipExpireTime: '',
        updateTime: '',
        createTime: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        username: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        avatar: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        gender: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        province: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        city: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        openid: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        intro: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        integral: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        exp: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        lastLoginIp: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        tagStr: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        birthday: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        postNum: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        fansNum: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        followNum: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        type: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        status: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        vipExpireTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updateTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        createTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/contents/user/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/contents/user/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
